package Server;

import module.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 长风无言
 */
public class ServerManageStudent {

    private static List<Student> students = new ArrayList<>();

    public static ObjectInputStream objectInput;
    public static ObjectOutputStream objectOutput;
    private static int checkflag;

    //读取学生信息
    public static void readFile(){
        try {
            objectInput = new ObjectInputStream(new FileInputStream("d:/test/listStudents.txt"));
            Object obj = objectInput.readObject();
            students = (List<Student>) obj;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != objectInput) {
                try {
                    objectInput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //写入文件更新学生信息
    public static void writeFile(){
        try {
            objectOutput = new ObjectOutputStream(new FileOutputStream("d:/test/listStudents.txt"));
            objectOutput.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != objectOutput) {
                try {
                    objectOutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //学生管理系统
    public static void work(ServerInitClose server) throws IOException, ClassNotFoundException {

        while (true) {
            readFile();
            String str = (String) server.getIn().readObject();
            String flag = str.split(";")[0];
            //增加学生
            if ("add".equals(flag)) {
                String name = str.split(";")[1];
                int num = Integer.parseInt(str.split(";")[2]);
                int age = Integer.parseInt(str.split(";")[3]);
                students.add(new Student(name,num,age));
                writeFile();
                server.getOut().writeObject("success");
                //删除学生
            } else if ("delete".equals(flag)) {
                checkflag = 0;
                int num = Integer.parseInt(str.split(";")[1]);
                Iterator<Student> iterator = students.iterator();
                // 判断是否还有迭代对象
                while (iterator.hasNext()) {
                    // 获取学生对象
                    Student student = iterator.next();
                    // 判断学生对象的学号和输入的学号是否相同
                    if (student.getNum() == num) {
                        iterator.remove();
                        writeFile();
                        System.out.println("删除成功");
                        checkflag = 1;
                    }
                }
                if(0 == checkflag){
                    server.getOut().writeObject("fail");
                }else {
                    server.getOut().writeObject("success");
                }
                //查询学生
            } else if ("find".equals(flag)) {
                checkflag = 0;
                int num = Integer.parseInt(str.split(";")[1]);
                for (Student student : students) {
                    if (num == student.getNum()) {
                        server.getOut().writeObject("success");
                        server.getOut().writeObject(student);
                        System.out.println("查找成功！");;
                        checkflag = 1;
                    }
                }server.getOut().writeObject("end");
                if(0 == checkflag){
                    server.getOut().writeObject("fail");
                }
                //修改学生信息
            } else if ("modify".equals(flag)) {
                checkflag = 0;
                int num = Integer.parseInt(str.split(";")[1]);
                String  name = str.split(";")[2];
                int newnum = Integer.parseInt(str.split(";")[3]);
                int age = Integer.parseInt(str.split(";")[4]);
                for (Student student : students) {
                    // 判断学生对象的学号是否为要修改信息的学生的学号
                    if (student.getNum() == num) {
                        student.setNum(newnum);
                        student.setName(name);
                        student.setAge(age);
                        checkflag = 1;
                        writeFile();
                    }
                }if(0 == checkflag){
                    server.getOut().writeObject("fail");
                }else {
                    server.getOut().writeObject("success");
                }
                //遍历学生信息
            } else if ("for".equals(flag)) {
                server.getOut().writeObject(students);
                System.out.println("发送完毕！");
            }else if("end".equals(flag)){
                return;
                //增添学生账号
            }else if("addaccount".equals(flag)){
                checkflag = 0;
                String account = str.split(";")[1];
                String password = str.split(";")[2];
                int num = Integer.parseInt(str.split(";")[3]);
                for (Student student : students) {
                    if(num == student.getNum()) {
                        student.setAccount(account);
                        student.setPassword(password);
                        writeFile();
                        checkflag = 1;
                    }
                }if(0 == checkflag){
                    server.getOut().writeObject("fail");
                }else {
                    server.getOut().writeObject("success");
                }
            }
        }
    }

}
